﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace MvdDonk.Bomberman.Entities
{
    public class Entity
    {
        protected const int TileMargin = 12;

        public Guid Id { get; private set; }
        public Texture2D Sprite { get; set; }
        public Vector2 Position { get; set; }
        public float Speed { get; protected set; }
        public double TimeAlive { get; set; }
        public double TimeToLive { get; protected set; }
        public bool IsAlive { get; protected set; }
        public bool IsDestroying { get; set; }

        public Vector2 MovementVector;


        protected int currentFrame = 0;
        protected int spriteWidth = 32;
        protected int spriteHeight = 48;

        public Entity(Texture2D sprite, Vector2 position)
        {
            Id = Guid.NewGuid();
            IsAlive = true;
            IsDestroying = false;
            Sprite = sprite;
            Position = position;

            TimeAlive = 0;
            TimeToLive = 0;
            Speed = 5f;
        }
            
        public virtual void Update(GameTime gameTime, TiledLib.Map map)
        {
            TimeAlive += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (TimeToLive > 0 && !IsDestroying && TimeAlive >= TimeToLive)
            {
                IsDestroying = true;
                Destroy();
            }

            if (!IsAlive)
                Bomberman.Instance.EntityManager.RemoveEntity(this);
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Sprite, Position, Color.White);
        }

        public virtual void Destroy()
        {
            IsAlive = false;
        }
    }
}
